package com.jic.product.controller.manage;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.common.util.BeanUtil;
import com.jic.product.entity.ProductPriceAudit;
import com.jic.product.manage.request.ProductPriceAuditRequest;
import com.jic.product.manage.response.ProductPriceAuditResponse;
import com.jic.product.service.ProductPriceAuditService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by WS on 2020/1/13  10: 55
 * 商品价格变更审核（商家，店铺）
 */
@RestController
@RequestMapping("/productPriceAudit")
@Api(value = "商品价格变更审核controller", tags = {"商品价格变更审核"})
public class ProductPriceAuditController {

    @Autowired
    ProductPriceAuditService productPriceAuditService;

    /*
     *  商家商品价格变更审核条件查询
     *
     * */
    @ApiOperation(value = "商品价格变更审核信息条件查询")
    @ResponseBody
    @PostMapping(value = "/queryProductPriceAudit")
    public RestResult query(@RequestBody ProductPriceAuditRequest productPriceAuditRequest) {
        List<ProductPriceAudit> productPriceAudit = null;
        try {
            ProductPriceAudit pPriceAudit = BeanUtil.copy(productPriceAuditRequest, ProductPriceAudit.class);
            productPriceAudit = productPriceAuditService.query(pPriceAudit);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResult.error("9999", e.getLocalizedMessage());
        }

        return RestResult.success(productPriceAudit);
    }

    /*
     * 分页查询
     * */
    @ApiOperation(value = "商品价格变更审核信息")
    @ResponseBody
    @PostMapping(value = "/queryPage")
    public RestResult<PageResult<ProductPriceAuditResponse>> queryPage(@RequestBody ProductPriceAuditRequest productPriceAuditRequest) {

        Page pageQuery = new Page();
        pageQuery.setLimit(productPriceAuditRequest.getLimit());
        pageQuery.setStart(productPriceAuditRequest.getStart());

        ProductPriceAudit productPriceAudit = BeanUtil.copy(productPriceAuditRequest, ProductPriceAudit.class);
        PageResult<ProductPriceAuditResponse> result = productPriceAuditService.queryPage(productPriceAudit, pageQuery);

        List<ProductPriceAuditResponse> productPriceAuditResponseList = result.getRows().stream().map(temp -> {
            ProductPriceAuditResponse productPriceAuditResponse = BeanUtil.copy(temp, ProductPriceAuditResponse.class);
            return productPriceAuditResponse;
        }).filter(item -> item != null).collect(Collectors.toList());

        PageResult<ProductPriceAuditResponse> pageResult = new PageResult<>();
        pageResult.setRows(productPriceAuditResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    /*
     *  商品价格变更审核（状态:0待审核;1审核通过;2审核拒绝;3:已取消）
     *
     * */
    @ApiOperation(value = "商品价格变更审核")
    @ResponseBody
    @PostMapping(value = "/updateProductPriceAudit")
    public RestResult updateProductPriceAudit(@RequestBody ProductPriceAuditRequest productPriceAuditRequest) {
        ProductPriceAudit pPriceAudit = BeanUtil.copy(productPriceAuditRequest, ProductPriceAudit.class);
        return RestResult.success(productPriceAuditService.update(pPriceAudit));
    }

    /*
     *  商品价格变更批量审核（状态:0待审核;1审核通过;2审核拒绝;3:已取消）
     *
     * */
    @ApiOperation(value = "商品价格变更批量审核")
    @ResponseBody
    @PostMapping(value = "/updateProductPriceAuditList")
    public RestResult updateProductPriceAuditList(@RequestBody ProductPriceAuditRequest productPriceAuditRequest) {
        RestResult restResult = new RestResult<>();
        try {
            ProductPriceAudit pPriceAudit = BeanUtil.copy(productPriceAuditRequest, ProductPriceAudit.class);
            for (ProductPriceAudit productPriceAudit1 : pPriceAudit.getProductPriceAuditList()) {
                long flag = productPriceAuditService.update(productPriceAudit1);
                if (flag == 1) {
                    restResult = RestResult.success(restResult);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            restResult = RestResult.error("9999", e.getLocalizedMessage());
        }
        return restResult;
    }

}
